route_n = '''[centos@ip-172-31-47-231 ~]$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.31.32.1     0.0.0.0         UG    0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
172.31.32.0     0.0.0.0         255.255.240.0   U     0      0        0 eth0'''

import re

list1 = route_n.split('\n')

route_n_list = list1[2:-1]

# print(route_n_list)

for x in route_n_list:
    # print(x.split())
    if x.split()[3] =='UG':
        print('网关为:' + x.split()[1])